package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private IStatementService statementService;


    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }


    @Override
    public void save(Appointment appointment) {
        // 添加需要创建时间,由于有默认值，所以不用设置
        // appointment.setCreateTime(new Date());
        appointmentMapper.insert(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Appointment appointment) {
        // 只有状态在预约中的才可以进行修改
        Appointment oldObj = this.get(appointment.getId());
        if (!Appointment.STATUS_APPOINTMENT.equals(oldObj.getStatus())){
            throw new BusinessException("只有处于预约中的预约单才可以进行修改");
        }
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public void delete(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            appointmentMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }

    @Override
    public void cancel(Long id) {
        // 进行合理性校验
        // 只有状态在预约中的才可以进行修改
        Appointment oldObj = this.get(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(oldObj.getStatus())){
            throw new BusinessException("只有处于预约中的预约单才可以进行取消");
        }
        appointmentMapper.changeStatus(id,Appointment.STATUS_CANCEL);
    }

    @Override
    public void arrival(Long id) {
        // 进行合理性校验
        // 只有状态在预约中的才可以进行修改
        Appointment oldObj = this.get(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(oldObj.getStatus())){
            throw new BusinessException("只有处于预约中的预约单才可以进行到店操作");
        }
        appointmentMapper.changeArrival(id,Appointment.STATUS_ARRIVAL,new Date());
    }

    @Override
    public Statement generateStatement(Long appointmentId) {
        // 进性合理性校验
        // 只有状态在到店或者已支付才可以生成结算单
        Appointment oldObj = this.get(appointmentId);
        if (!(Appointment.STATUS_ARRIVAL.equals(oldObj.getStatus()) || Appointment.STATUS_SETTLE.equals(oldObj.getStatus()))){
            // 不是到店或者已支付的状态
            throw new BusinessException("不是到店或者已支付的状态，不能生成结算单");
        }
        // 根据id查询结算单对象
        Statement statement = statementService.getByAppointmentId(appointmentId);
        if (statement == null){
            // 说明没有生成过结算单
            statement = new Statement();
            statement.setAppointmentId(appointmentId); // 关联预约单id
            statement.setCarSeries(oldObj.getCarSeries()); // 汽车分类
            statement.setActualArrivalTime(oldObj.getActualArrivalTime()); // 到店时间
            statement.setCreateTime(new Date()); // 创建记录时间
            statement.setCustomerName(oldObj.getCustomerName()); // 客户名称
            statement.setCustomerPhone(oldObj.getCustomerPhone()); // 客户手机号码
            statement.setInfo(oldObj.getInfo()); // 备注信息
            statement.setServiceType(oldObj.getServiceType()); // 服务类型
            statement.setLicensePlate(oldObj.getLicensePlate()); // 车牌号码
            // 保存结算单
            statementService.save(statement);

        }else{
            // 说明已经生成过结算单了
        }
        return statement;
    }

    @Override
    public void updateStatus(Long appointmentId, Integer status) {
        appointmentMapper.changeStatus(appointmentId,status);
    }

}
